package com.Xht.www.dao;

import com.Xht.www.po.WalletTransactionLogPO;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class WalletTransactionLogDAO extends BaseDAO {

    // 插入账单流水
    public boolean insert(WalletTransactionLogPO log) {
        if (!isValid(log)) {
            return false;
        }
        String sql = "INSERT INTO wallet_transaction_log (user_id, wallet_id, transaction_type, transaction_amount, transaction_time) VALUES (?,?,?,?,?)";
        Connection conn = getConnection();
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, log.getUserId());
            pstmt.setInt(2, log.getWalletId());
            pstmt.setString(3, log.getTransactionType());
            pstmt.setBigDecimal(4, log.getTransactionAmount());
            pstmt.setTimestamp(5, log.getTransactionTime());
            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            releaseConnection(conn);
        }
    }

    // 删除账单流水操作
    public boolean delete(int logId) {
        if (logId <= 0) {
            return false;
        }
        String sql = "DELETE FROM wallet_transaction_log WHERE log_id = ?";
        Connection conn = getConnection();
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, logId);
            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            releaseConnection(conn);
        }
    }

    // 查询账单流水操作
    public WalletTransactionLogPO select(int logId) {
        if (logId <= 0) {
            return null;
        }
        String sql = "SELECT * FROM wallet_transaction_log WHERE log_id = ?";
        Connection conn = getConnection();
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, logId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    WalletTransactionLogPO log = new WalletTransactionLogPO();
                    log.setLogId(rs.getInt("log_id"));
                    log.setUserId(rs.getInt("user_id"));
                    log.setWalletId(rs.getInt("wallet_id"));
                    log.setTransactionType(rs.getString("transaction_type"));
                    log.setTransactionAmount(rs.getBigDecimal("transaction_amount"));
                    log.setTransactionTime(rs.getTimestamp("transaction_time"));
                    return log;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            releaseConnection(conn);
        }
        return null;
    }

    // 更新账单流水操作
    public boolean update(WalletTransactionLogPO log) {
        if (!isValid(log)) {
            return false;
        }
        String sql = "UPDATE wallet_transaction_log SET user_id = ?, wallet_id = ?, transaction_type = ?, transaction_amount = ?, transaction_time = ? WHERE log_id = ?";
        Connection conn = getConnection();
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, log.getUserId());
            pstmt.setInt(2, log.getWalletId());
            pstmt.setString(3, log.getTransactionType());
            pstmt.setBigDecimal(4, log.getTransactionAmount());
            pstmt.setTimestamp(5, log.getTransactionTime());
            pstmt.setInt(6, log.getLogId());
            int rows = pstmt.executeUpdate();
            return rows > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            releaseConnection(conn);
        }
    }

    // 检验输入是否合法有效
    private boolean isValid(WalletTransactionLogPO log) {
        if (log == null) {
            return false;
        }
        if (log.getUserId() == null || log.getUserId() <= 0) {
            return false;
        }
        if (log.getWalletId() == null || log.getWalletId() <= 0) {
            return false;
        }
        if (log.getTransactionType() == null || log.getTransactionType().isEmpty()) {
            return false;
        }
        if (log.getTransactionAmount() == null || log.getTransactionAmount().compareTo(BigDecimal.ZERO) <= 0) {
            return false;
        }
        if (log.getTransactionTime() == null) {
            return false;
        }
        return true;
    }

    // 根据用户 ID 查询账单流水
    public List<WalletTransactionLogPO> getTransactionLogByUserId(int userId) {
        List<WalletTransactionLogPO> transactionLog = new ArrayList<>();
        String sql = "SELECT * FROM wallet_transaction_log WHERE user_id = ?";
        Connection conn = getConnection();
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            try (ResultSet rs = pstmt.executeQuery()) {
                while (rs.next()) {
                    WalletTransactionLogPO log = new WalletTransactionLogPO();
                    log.setLogId(rs.getInt("log_id"));
                    log.setUserId(rs.getInt("user_id"));
                    log.setWalletId(rs.getInt("wallet_id"));
                    log.setTransactionType(rs.getString("transaction_type"));
                    log.setTransactionAmount(rs.getBigDecimal("transaction_amount"));
                    log.setTransactionTime(rs.getTimestamp("transaction_time"));
                    transactionLog.add(log);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            releaseConnection(conn);
        }
        return transactionLog;
    }
}